// components/activity-product-edit/index.js
import componentBehavior from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from '../../../constants/common';
import { ACTIVITY_OWNER_VALUE, ACTIVITY_STATUS_VALUE } from '../../../constants/activity';
import { swapArr, arr2Obj, formatDecimal, deepClone } from '../../../common/utils';
import { tipToast } from '../../../common/env';

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        owner: {
            type: String,
            value: ACTIVITY_OWNER_VALUE.brand, // brand || spokesman
        },
        activityInfo: {
            type: Object,
            value: {},
        },
    },
    observers: {
        'activityInfo': function (val) {
            if (val) {
                const modifyFlag = !val.processStatus || val.processStatus === ACTIVITY_STATUS_VALUE.draft;
                this.setData({ modifyFlag });
            }
            if (val.activityProductWithSkus && val.activityProductWithSkus.length) {
                const bonusMap = arr2Obj(val.consignment.consignmentBonusSettings, 'productId');
                const cloneVal = deepClone(val);
                const activityProductWithSkus = cloneVal.activityProductWithSkus.map(ps => {
                    const hasSkus = ps.priceMin != ps.priceMax;
                    let bonusRate = bonusMap[ps.productId] ? bonusMap[ps.productId].bonusRate : 0;
                    if (bonusRate) bonusRate = bonusRate / 10;
                    ps.bonusStr = bonusRate ? `¥ ${formatDecimal((ps.priceMin / 100) * bonusRate / 100)}${hasSkus ? '-' + formatDecimal((ps.priceMax / 100) * bonusRate / 100) : ''}` : `¥ ${formatDecimal((ps.priceMin / 100) * 0)}`;
                    if (ps.skus.some(s => s.maximum <= 0)) {
                        ps.maximumTotal = '';
                    } else {
                        ps.maximumTotal = ps.skus.reduce((s, c) => s + parseFloat(c.maximum <= 0 ? 0 : c.maximum), 0);
                    }
                    return ps;
                });
                this.setData({ activityProductWithSkus });
            } else {
                this.setData({ activityProductWithSkus: [] });
            }
        },
    },
    data: {
        ACTIVITY_OWNER_VALUE,
        activityProductWithSkus: [],
        modifyFlag: true,
    },
    methods: {
        _handleChange(data) {
            const { activityInfo, activityProductWithSkus } = this.data;
            activityInfo.activityProductWithSkus = activityProductWithSkus.map(ps => {
                delete ps.maximumTotal;
                return ps;
            });
            this.triggerEvent('change', {
                ...activityInfo,
                ...data,
            });
        },
        handleUp(e) {
            const { index } = e.currentTarget.dataset;
            let { activityProductWithSkus } = this.data;
            if (index === 0) return;
            activityProductWithSkus = swapArr(activityProductWithSkus, index, index - 1);
            this._handleChange({ activityProductWithSkus });
        },
        handleDown(e) {
            const { index } = e.currentTarget.dataset;
            let { activityProductWithSkus } = this.data;
            if (index === activityProductWithSkus.length - 1) return;
            activityProductWithSkus = swapArr(activityProductWithSkus, index, index + 1);
            this._handleChange({ activityProductWithSkus });
        },
        handleRemove(e) {
            if (!this.data.modifyFlag) {
                tipToast('活动已发布，不能删除商品');
                return;
            }
            const { index, productId } = e.currentTarget.dataset;
            const { consignment } = this.data.activityInfo;
            let { activityProductWithSkus } = this.data;
            activityProductWithSkus.splice(index, 1);
            let idx = consignment.consignmentBonusSettings.findIndex(i => i.productId === productId);
            idx > -1 && consignment.consignmentBonusSettings.splice(idx, 1);
            this._handleChange({ activityProductWithSkus, consignment });
        },
        handleShow(e) {
            const { index, isShow } = e.currentTarget.dataset;
            let { activityProductWithSkus } = this.data;
            activityProductWithSkus[index].isShow = !isShow;
            this._handleChange({ activityProductWithSkus });
        },
        handleModify(e) {
            const { productId } = e.currentTarget.dataset;
            this.triggerEvent('modify', productId);
        },
        handleAdd(e) {
            if (!this.data.modifyFlag) {
                tipToast('活动已发布，不能添加商品');
                return;
            }
            this.triggerEvent('add');
        },
    }
})
